package br.com.moxeon.control;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.moxeon.model.Accounts;
import br.com.moxeon.model.cash.Throw;
import br.com.moxeon.model.cash.ThrowOperation;
import br.com.moxeon.model.enums.AccountSituation;
import br.com.moxeon.model.enums.CheckSituation;
import br.com.moxeon.model.enums.TypeThrowItem;
import br.com.moxeon.model.enums.TypeThrowOperation;
import br.com.moxeon.model.enums.TypeThrowTransaction;
import br.com.moxeon.model.financialaccounts.FinancialAccount;
import br.com.moxeon.model.financialitems.Check;
import br.com.moxeon.model.financialitems.DebitInAccount;
import br.com.vexillum.util.Return;

@Service
@Scope("prototype")
@Deprecated
public class SingleThrowOperationController extends ThrowOperationController {

	public Return executeOperation() {
		Return ret = super.executeOperation();
		entity.setTipoOperacao(TypeThrowOperation.LANCAMENTO_AVULSO);
		entity.setPayableAccounts(null);
		entity.setReceivableAccounts(null);
		// /Poderia haver um método para tratar lançamentos
		// e um método para tratar cheques
		Throw financialThrow = (Throw) getData().get("throw");
		if (financialThrow.getItem() instanceof Check) {
			Check check = (Check) financialThrow.getItem();
			check.setValue(financialThrow.getValue());
			check.setMaturity((Date) getData().get("checkMaturity"));
			check.setIssueDate((Date) getData().get("issueDate"));
		}
		ArrayList<Throw> financialThrows = new ArrayList<Throw>();
		financialThrows.add(financialThrow);
		entity.setListThorws(financialThrows);
		financialThrow.setOperation(entity);
		ret.concat(save());
		return ret;
	}

	public SingleThrowOperationController() {
		super();
	}
	


	@SuppressWarnings({ "static-access", "unchecked" })
	public Return reverseOperation() {
		Return ret = new Return(false);
		TypeThrowOperation tipoOperacao = entity.getTipoOperacao();
		List<Throw> listThorws = entity.getListThorws();
		List<Accounts> listPayableAccounts = entity.getPayableAccounts();

		if (tipoOperacao == entity.getTipoOperacao().LANCAMENTO_AVULSO) {
			for (Throw t : listThorws) {
				t.setActive(false);
				if (t.getItem() != null) {
					TypeThrowItem financialItem = t.getTypeThrowItem();
					if (financialItem == TypeThrowItem.CHEQUE) {
						Check check = (Check) t.getItem();
						check.setValue(null);
						check.setMaturity(null);
						check.setIssueDate(null);
						check.setSituation(CheckSituation.ABERTO);
						check.setObservation(null);
					} else if (financialItem == TypeThrowItem.DEBITO) {
						DebitInAccount debitInAccount = (DebitInAccount) t
								.getItem();
						debitInAccount.setActive(false);
						debitInAccount.setValue(null);
						debitInAccount.setDebitDay(null);
					} else if (financialItem == TypeThrowItem.DINHEIRO) {

					} else if (financialItem == TypeThrowItem.CREDIARIO) {

					}
				}
			}
			ret = super.reverseOperation();
			return ret;

		} else if (tipoOperacao == entity.getTipoOperacao().BAIXA_DESPESA) {
			for (Accounts a : listPayableAccounts) {
				a.setAccountSituation(AccountSituation.ABERTA);
				ThrowOperation to = a.getOperation();
				
				for (Throw t : listThorws) {
					t.setActive(false);
					TypeThrowItem financialItem = t.getTypeThrowItem();
					if (t.getItem() != null) {
						if (financialItem == TypeThrowItem.CHEQUE) {
							Check check = (Check) t.getItem();
							check.setValue(null);
							check.setMaturity(null);
							check.setIssueDate(null);
							check.setSituation(CheckSituation.ABERTO);
							check.setObservation(null);
						} else if (financialItem == TypeThrowItem.DEBITO) {
							DebitInAccount debitInAccount = (DebitInAccount) t
									.getItem();
							debitInAccount.setActive(false);
							debitInAccount.setValue(null);
							debitInAccount.setDebitDay(null);
						} else if (financialItem == TypeThrowItem.DINHEIRO) {
							DebitInAccount debitInAccount = (DebitInAccount) t
									.getItem();
							debitInAccount.setActive(false);
							debitInAccount.setValue(null);
							debitInAccount.setDebitDay(null);
						} else if (financialItem == TypeThrowItem.CREDIARIO) {
						}
					}
				}
				ret = super.reverseOperation();
				return ret;
			}
		} else if (tipoOperacao == entity.getTipoOperacao().PERMUTA) {

		} else if (tipoOperacao == entity.getTipoOperacao().TRANSFERENCIA) {
			for (Throw t : listThorws) {
				t.setActive(false);
				FinancialAccount f = t.getFinancialAccount();
				
				if (t.getItem() != null) {
					TypeThrowTransaction typeTransaction = t.getType();

					DebitInAccount debitInAccount = (DebitInAccount) t
							.getItem();
					debitInAccount.setActive(false);
					debitInAccount.setValue(null);
					debitInAccount.setDebitDay(null);

					if (typeTransaction == TypeThrowTransaction.ENTRADA) {
						f.setActive(false);
					} else if (typeTransaction == TypeThrowTransaction.SAIDA) {
						f.setActive(false);
					}
				}
				ret = super.reverseOperation();
				return ret;
			}

		} else if (tipoOperacao == entity.getTipoOperacao().REPASSE) {

		} else if (tipoOperacao == entity.getTipoOperacao().RECEBIMENTO_RECEITA) {

		}
		return ret;
	}
}
